<?php
namespace app\wechat\controller;


use think\Db;
use think\View;
use Jssdk\JSSDK;


class ROrders extends Common
{
    public function index()
    {
        $status = input('param.status', 0);
        $this->assign('status',$status);
        return view();
    }

    public function index_today()
    {
        $status = input('param.status', 0);
        $this->assign('status',$status);
        return view();
    }
    public function get_order_server_list_today()
    {
        if (request()->isAjax()) {
            $member_id = session('member_id');
            if (!$member_id) {
                return json(['code' => 0, 'msg' => '系统繁忙']);
            }
            $param['member_id'] = $member_id;
            $param['model'] = 'order_server_deal';
            $param['status'] = input('param.status', 0);
            $data = call_interface('repair_order_server_list',$param);
            //dump($data);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('r_orders/order_list_tpl');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view('base/error');
    }

    public function get_order_server_list()
    {
        if (request()->isAjax()) {
            $member_id = session('member_id');
            if (!$member_id) {
                return json(['code' => 0, 'msg' => '系统繁忙']);
            }
            $param['member_id'] = $member_id;
            $param['model'] = 'order_server_deal';
            $param['status'] = input('param.status', 0);
            $data = call_interface('repair_order_server_list',$param);
            //dump($data);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('r_orders/order_list_tpl');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view('base/error');
    }

    public function order_info()
    {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){
            session('member_id',@intval($_COOKIE["PJ"]));
            $signPackage = [
                'appId' => '',
                'timestamp' => '',
                'nonceStr' => '',
                'signature' => ''
            ];
        }else{
            $jssdk = new JSSDK(config('wechat.appid'), config('wechat.appsecret'));
            $signPackage = $jssdk->GetSignPackage();
        }
        $this->assign('signPackage', $signPackage);

        $order_id = intval(input('param.id',0));
        $order = db('order_server')->alias('o')
            ->field('o.*,si.item_name,rmr.headimgurl r_headimgurl, mmr.headimgurl m_headimgurl, r.repair_name, r.lat r_lat, r.lon r_lon, r.phone r_phone, oa.id as oa_id, car.name as car_name')
            ->join('mob_server_items si', 'si.id = o.type', 'LEFT')
            ->join("mob_repair r", "o.repair_id = r.id", 'LEFT')
            ->join("mob_member_role_info rmr", "r.member_id = rmr.member_id and rmr.type = 1", 'LEFT')
            ->join("mob_member_role_info mmr", "o.member_id = mmr.member_id and mmr.type = 0", 'LEFT')
            ->join("mob_order_austin oa", "oa.order_id = o.id AND oa.type = 2", 'LEFT')
            ->join("mob_my_car car", "car.member_id = o.member_id", 'LEFT')
            ->where("o.id",$order_id)->order("car.is_default desc")->find();
        $this->assign('order', $order);

        return view();
    }

    // 订单产品列表
    public function server_order_goods() {
        $order_id = input('param.order_id');
        $order = db('order_server')->alias('o')->field('o.apply_person,o.phone,o.order_sn, o.order_price, o.status, o.add_time, o.after_sale_status, o.finish_time, rmr.headimgurl,  rmr.nickname , r.repair_name,  r.logo')
            ->join("mob_repair r", "o.repair_id = r.id", 'LEFT')
            ->join("mob_member_role_info rmr", "o.member_id = rmr.member_id and rmr.type = 0", 'LEFT')
            ->where("o.id",$order_id)->find();
        $this->assign('order', $order);

        $order_goods = db('order_product_server')->alias('op')->field('op.*, oa.status, oa.audit_type')
            ->join('mob_order_austin oa', 'oa.product_id = op.id AND oa.order_id = op.order_id', 'LEFT')
            ->where('op.order_id', $order_id)->select();
        $this->assign('order_goods', $order_goods);
        return view();
    }

    // 取消订单
    public function cancel_order() {
        $id = input('param.id');
        $model = input('param.model');
        $member_id = session('member_id');
        try {
            db($model)->where('id', $id)->where('member_id', $member_id)->update(['status' => 9]);
            return json(['code' => 200, 'msg' => 'success']);
        } catch (\Exception $e) {
            return json(['code' => 0, 'msg' => $e->getMessage()]);
        }
    }

    // 订单完成
    public function affirm_finish() {
        $id = input('param.id');
        $model = input('param.model');
        $member_id = session('member_id');
        $repair_id = db('repair')->where('member_id',$member_id)->value('id');

        $param = [
            'repair_id' => $repair_id,
            'status' => 4,
            'order_id' => $id,
            'model' => $model
        ];
        $data = call_interface('repair_update_order_server_status',$param);

        return json($data);

    }

//=============================销售采购============================

    // 维修店销售订单产品列表
    public function server_deal_goods() {
        $order_id = input('param.order_id');
        $order = db('order_server_deal')->alias('o')->field('o.id,o.apply_person,o.phone,o.order_sn, o.order_price, o.status, o.add_time, o.after_sale_status, o.finish_time, rmr.logo as m_logo,  rmr.repair_name as m_repair_name , r.repair_name,  r.logo')
            ->join("mob_repair r", "o.repair_id = r.id", 'LEFT')
            ->join("mob_repair rmr", "o.repair_id_to = rmr.id ", 'LEFT')
            ->where("o.id",$order_id)->find();
        $this->assign('order', $order);

        $order_goods = db('order_product_server_deal')->alias('op')->field('op.*')
            ->where('op.order_id', $order_id)->select();
        $this->assign('order_goods', $order_goods);
        return view();
    }

    // 维修店销售订单产品列表(支付)
    public function server_deal_goods_pay() {
        $order_id = input('param.order_id');
        $order = db('order_server_deal')->alias('o')->field('o.id,o.apply_person,o.phone,o.order_sn, o.order_price, o.status, o.add_time, o.after_sale_status, o.finish_time, rmr.logo as m_logo,  rmr.dealer_name as m_repair_name , r.repair_name, r.logo, r.phone as r_phone')
            ->join("mob_repair r", "o.obj_id = r.id", 'LEFT')
            ->join("mob_dealer rmr", "o.dealer_id = rmr.id ", 'LEFT')
            ->where("o.id",$order_id)->find();
        $this->assign('order', $order);

        $order_goods = db('order_product_server_deal')->alias('op')->field('op.*')
            ->where('op.order_id', $order_id)->select();
        $this->assign('order_goods', $order_goods);
        return view();
    }

    // 经销商销售采购订单产品列表
    public function deal_order(){
        $status = input('param.status', 0);
        $this->assign('status',$status);
        return view();
    }
    // 维修店采购订单产品列表
    public function deal_order_repair(){
        $status = input('param.status', 0);
        $this->assign('status',$status);
        return view();
    }

    public function get_order_server_list_deal()
    {
        if (request()->isAjax()) {
            $member_id = session('member_id');
            if (!$member_id) {
                return json(['code' => 0, 'msg' => '系统繁忙']);
            }
            $param['member_id'] = $member_id;
            $param['model'] = 'order_server_deal';
            $param['status'] = input('param.status', 0);
            $data = call_interface('repair_order_server_list_deal',$param);
            //dump($data);
            /**/
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            if(!$param['status']){
                $tpl = $view->fetch('r_orders/deal_list_tpl');
            }else{
                $tpl = $view->fetch('r_orders/deal_list_tpl_pay');
            }
            $data['tpl'] = $tpl;
            return json($data);

        }
        return view('base/error');
    }
    


}
